package com.bds.portal.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;

import com.bds.portal.util.VerifyCodeUtils;

@org.springframework.stereotype.Controller
public class VerifyCodeAction {

	private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(VerifyCodeAction.class);

	@RequestMapping("/action/verify/verifyCode")
	public void verifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.setContentType("image/jpeg");

			// 生成随机字串
			String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
			// 存入会话session
			HttpSession session = request.getSession(true);
			// 删除以前的
			session.removeAttribute("verifyCode");
			session.setAttribute("verifyCode", verifyCode.toLowerCase());
			// 生成图片
			int w = 200, h = 80;
			VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
		} catch (Exception e) {
			logger.error("获取验证码异常", e);
		}

	}

}
